export default {
  data () {
    return {
      dialogVisibleForGroup: false, //新增分组
      tabIndexForGroup: 1, //(1:创建分组;2:选择停车场)
      addGroupForm:{name:'',remark:''},
      groupRules:{
        name: [
          { required: true, message: '请输入分组名称', trigger: 'blur' },
          { min: 2, max: 30, message: '长度在 2 到 30 个字符', trigger: 'change' }
        ],
        remark: [
          { min: 1, max: 50, message: '长度在 1 到 50 个字符', trigger: 'change' }
        ]
      },

      groupForm:{id:"",name:"",remark:""},
      groupId:"",
      rules:{
        name: [
          { required: true, message: '请输入角色名称', trigger: 'blur' },
          { min: 2, max: 30, message: '长度在 2 到 30 个字符', trigger: 'change' }
        ],
        remark: [
          // { required: true, message: '请输入描述', trigger: 'blur' },
          { min: 1, max: 50, message: '长度在 1 到 50 个字符', trigger: 'change' }
        ]
      },
      userRow: {
        total: 0,
        theadsInfo: [
          { index: 0, isShow: false, prop: 'reno', text: '序号', width: 68 },
          { index: 1, isShow: false, prop: 'id', text: 'ID', minWidth: 110 },
          { index: 2, isShow: false, prop: 'loginId', text: '用户名', minWidth: 155 },
          { index: 3, isShow: false, prop: 'nickName', text: '昵称', minWidth: 110 },
          { index: 4, isShow: false, prop: 'mobile', text: '手机号', minWidth: 110 }
        ],
        tbodysInfo: []
      }
    }
  },
  created () {
    
  },
  methods: {
    toOpenAddGroupDialog(){
      this.addGroupForm.name = ''
      this.addGroupForm.remark = ''
      this.dialogVisibleForGroup=true;
      this.initParkTreeForAddGroup('parkTree')
    },
    initParkTreeForAddGroup (treeId) {//新增分组-初始化停车场树
      API.platform_manage.GetParkingGroupAreaParkTree({}).then(res => {
        $.fn.zTree.destroy(treeId);
        var settings = {
          check: {
              enable: true
          },
          data: {
              simpleData: {
                  enable: true
              }
          },
          view: {
              showIcon: false,
              showLine: false,
          }
        };
        $.fn.zTree.init($("#"+treeId), settings, res.data.children);

        var treeObj = $.fn.zTree.getZTreeObj(treeId);
        treeObj.expandAll(true);
      },err => {});
    },
    bindParkingGroupForAddGroup (treeId){ //新增分组-绑定停车场
      var self = this;
      var tree = $.fn.zTree.getZTreeObj(treeId);
      var nodes = tree.getCheckedNodes(true);
      var parkList = [];
      for (var i = 0; i < nodes.length; i++) {
          if (!nodes[i].isArea) {
              parkList.push(nodes[i].value);
          }
      }
      if (parkList.length <= 0) {
          self.showToast('请至少选择一个停车场', 0);
          return false;
      }
      return parkList;
    },
    saveAddGroupModal(formName){ //新增分组-保存新增分组
      let self = this
      this.$refs[formName].validate((valid)=>{
        if(!valid){
          return false;
        }else{
          let params = $.extend({}, self.addGroupForm);
          let parkIdList = self.bindParkingGroupForAddGroup('parkTree');
          params.parkIdList = parkIdList
          API.platform_manage.SaveParkingGroupData(params).then(res =>{
            if(res.status==1){
              this.showToast("操作成功",1);
              this.dialogVisibleForGroup = false
              //获取分组列表
              self.getList()
            }else{
              this.showToast("操作失败",2);
            }
          }, err => {});
        }
      })
    },
    getList () {
      this.getParkingGroupList(this.searchParams);
    },
    getParkingGroupList (aparams) {//查询列表
      let self = this,
          params = {
            page: this.pageNumber,
            rp: this.pageSize
          };
      params = $.extend({}, params, aparams);
      API.platform_manage.GetParkingGroupListData(params)
          .then(res => {
            let data = res.data,
                list = data.rows;
            self.tbodysInfo = list;
            self.total = data.total;
          }, err => {})
    },
    toOpenAddGroup (modalName) {//打开新增
      this.groupForm.id = "";
      this.groupForm.name = "";
      this.groupForm.remark = "";
      this.toOpenModal(modalName);
    },
    toOpenEditGroup (row,modalName){//打开编辑
      this.groupForm.id = row.id;
      this.groupForm.name = row.name;
      this.groupForm.remark = row.remark;
      this.toOpenModal(modalName);
    },
    toOpenBindPark(row,modalName,treeId){//打开设置权限对话框
      let self = this;
      self.groupId = row.id;
      self.toOpenModal(modalName,1);
      self.initParkTree(treeId, row.parkIdList);
    },
    initParkTree (treeId,parkIdList) {//初始化权限树
      API.platform_manage.GetParkingGroupAreaParkTree({}).then(res => {
        $.fn.zTree.destroy(treeId);
        var settings = {
          check: {
              enable: true
          },
          data: {
              simpleData: {
                  enable: true
              }
          },
          view: {
              showIcon: false,
              showLine: false,
          }
        };
        $.fn.zTree.init($("#"+treeId), settings, res.data.children);

        var treeObj = $.fn.zTree.getZTreeObj(treeId);
        treeObj.expandAll(true);
        for (var i = 0; i < parkIdList.length; i++) {
          var node = treeObj.getNodeByParam("value", parkIdList[i]);
          if (node) {
            treeObj.checkNode(node, true, true);
          }
        }
      },err => {});
    },
    bindParkingGroup (treeId){//保存权限
      var self = this;
      var tree = $.fn.zTree.getZTreeObj(treeId);
      var nodes = tree.getCheckedNodes(true);
      var parkList = [];
      for (var i = 0; i < nodes.length; i++) {
          if (!nodes[i].isArea) {
              parkList.push(nodes[i].value);
          }
      }
      if (parkList.length <= 0) {
          self.showToast('请至少选择一个停车场', 0);
          return false;
      }
      var params = {
          'id': self.groupId,
          'parkIdList': parkList
      };
      API.platform_manage.BindParkingGroup(params).then(res => {
          if(res.status==1){
            self.getList();
            self.showToast("操作成功",1);
            self.closed('bindParkModal');
          }else{
            self.showToast("操作失败",2);
          }
      }, err => {});
    },
    filterMultiCheck (selectionArr) {//必须定义获取多列的方法
      let result = [];
        selectionArr.map((item)=>{
          result.push(item.id);
        });
      return result;
    },
    toOpenDeleteGroup(row){//打开删除对话框
      let self = this;
      this.showDialog("是否确认删除所选记录？",[],function(){
        API.platform_manage.DeleteParkingGroup({"id":row.id}).then(res => {
          if(res.status==1){
            self.getList();
            self.showToast("操作成功",1);
          }else{
            self.showToast("操作失败",2);
          }
        }, err =>{});
      },"温馨提示",function(){
        //取消时不操作
      });
    },
    toOpenUserDetail(row,modalName){//打开用户列表
      this.toOpenModal(modalName);
      this.getUserList(row);
    },
    getUserList(row){//获取用户列表
      let self = this,
          params = {
            id:row.id,
            page: 1,
            rp: 999999
          };
      self.userRow.tbodysInfo = []
      self.userRow.total = 0
      API.platform_manage.GetUserList(params)
          .then(res => {
            let data = res.data,
                list = data.rows;
            self.userRow.tbodysInfo = list;
            self.userRow.total = data.total;
          }, err => {})
    },
    saveParkingGroup(formName,modalName){//保存角色
      this.$refs[formName].validate((valid) => {
          if (!valid) {
            return false;
          }else{

            API.platform_manage.SaveParkingGroupData(this.groupForm).then(res =>{
              let sta = res.status;
              if(res.status==1){
                this.closed(modalName);
                this.getList();
                this.showToast("操作成功",1);
              }else{
                this.showToast("操作失败",2);
              }
            }, err => {});

          }
      });
    }
  }
}